<!-- pages/register.vue -->
<template>
  <div class="w-full bg-gray-100 flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
      <h2 class="text-2xl font-bold mb-6 text-center">注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="mb-4">
          <label for="username" class="block text-sm font-medium text-gray-700">用户名</label>
          <input type="text" placeholder="请输入用户名" nameid="username" v-model="username" class="Input mt-1 block w-full rounded-md shadow-sm " required>
        </div>
      
        <div class="mb-4">
          <label for="password" class="block text-sm font-medium text-gray-700">密码</label>
          <input type="password" placeholder="请输入密码" id="password" v-model="password" class="Input mt-1 block w-full  rounded-md shadow-sm " required>
        </div>
        <div class="mb-4">
          <label for="confirm-password" class="block text-sm font-medium text-gray-700">确认密码</label>
          <input type="password" placeholder="请确认密码" id="confirm-password" v-model="confirmPassword" class="Input mt-1 block w-full  rounded-md shadow-sm " required>
        </div>
        <button type="submit" class=" w-full bg-indigo-500 text-white py-2 px-4 rounded-md hover:bg-indigo-600 focus:outline-none focus:ring-2 focus:ring-indigo-500">注册</button>
      </form>
      <div class="mt-6 text-center">
        <span class="text-gray-600">已有账号？</span>
        <a href="/login" class="text-indigo-500 hover:text-indigo-600">立即登录</a>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { showNotification } from '@/services/notificationService';
definePageMeta({
  layout: 'empty',
});
import { register  } from '~/api/users';
import { useAuthStore } from '@/stores/auth';
const authStore = useAuthStore();
authStore.logout();

const username = ref('')
const password = ref('')
const confirmPassword = ref('')



const handleRegister = async () => {
  // 处理注册逻辑
  if (password.value !== confirmPassword.value) {
    showNotification('密码不一致')
    
    return
  }
  register({
    username: username.value,
    password: password.value,
  }).then((res) => {
    
    if (res.code === 200) {
      showNotification('注册成功,3s后跳转到登录页面', 'success')
      setTimeout(() => {
        navigateTo('/login')
      }, 3000);
    }
  })
}
</script>